//
//  PopoverAnimationMenager.swift
//  WeiBo(Swift)
//
//  Created by 周成龙 on 16/9/25.
//  Copyright © 2016年 ZCL. All rights reserved.
//

import UIKit

let kPopoverDidPresentNotification: String = "kPopoverDidPresentNotification"
let kPopoverDidDismissNotification: String = "kPopoverDidDismissNotification"

class PopoverAnimationMenager: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    
    private var isPresent = false
    var presentFrame = CGRect.zero
    
    // MARK: - UIViewControllerTransitioningDelegate
    
    // 转场动画对象
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?
    {
        let vc = PopoverPresentationController(presentedViewController: presented, presenting: presenting)
        vc.presentFrame = presentFrame
        return vc
    }
    
    // 转场动画出现对象
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresent = true
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: kPopoverDidPresentNotification), object: self)
        
        return self
    }
    
    // 转场动画消失对象
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: kPopoverDidDismissNotification), object: self)
        return self
    }
    
    // MARK: - UIViewControllerAnimatedTransitioning
    
    // 动画时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    // 实现动画
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        if isPresent {
            willPresendView(using: transitionContext)
        }else {
            willDismissView(using: transitionContext)
        }
    }
    
    private func willPresendView(using transitionContext: UIViewControllerContextTransitioning) {
        guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {
            return
        }
        
        transitionContext.containerView.addSubview(toView)
        toView.layer.anchorPoint = CGPoint(x: 0.0, y: 0.0)
        // 做动画
        toView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            toView.transform = CGAffineTransform.identity
        }) { (_) in
            transitionContext.completeTransition(true)
        }
    }
    
    private func willDismissView(using transitionContext: UIViewControllerContextTransitioning) {
        guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {
            return
        }
        // 需要注意 CGFloat 不准确的问题
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            fromView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0001)
        }) { (_) in
            transitionContext.completeTransition(true)
        }
    }
}
